﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using eDonkeyAPI.Models;
using System.Web;

namespace eDonkeyAPI.Controllers
{
    public class UserController : ApiController
    {
        //
        // GET: /User/
        [HttpGet]
        public void Index()
        {
            
        }


        [HttpGet]
        public void Save(string user, string url)
        {
            mydbEntities myDb = new mydbEntities();
            User userDb = myDb.Users.Where(u => u.User1 == user).FirstOrDefault();
            if (userDb != null)
            {
                userDb.URL = url;
            }
            else
            {
                userDb = new User();
                int id = myDb.Users.Count();
                userDb.Id = id + 1;
                userDb.User1 = user;
                userDb.URL = url;            
                myDb.Users.Add(userDb);
            }
            myDb.SaveChanges();
        }

        [HttpGet]
        public User Open(string user)
        {
            mydbEntities myDb = new mydbEntities();
            User userDb = myDb.Users.Where(u => u.User1 == user).FirstOrDefault();
            if (userDb == null)
                return null;
            User userObj = new User();
            //user.Id = 0;
            //user.User1 = user; 
            userObj.URL = userDb.URL;
            return userObj;
        }

    }
}
